package com.tledu.spring.service.impl;

import com.tledu.spring.domain.User;
import com.tledu.spring.dao.UserMapper;
import com.tledu.spring.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rupert-tears
 * @since 2021-07-08
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    /**
     * 注人持久成对象
     */
    private final UserMapper userMapper;

    @Override
    public Integer insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer delete(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User selectById(User user) {
        return userMapper.selectById(user);
    }


    @Override
    public List<User> selectByName(String name) {
        if ("".equals(name) || name == null){
            List<User> users = userMapper.selectList(null);
            return users;
        }else {
            // 自定义查询
            HashMap<String, Object> map = new HashMap<>(20);
            map.put("name",name);
            List<User> users = userMapper.selectByMap(map);
            return users;
        }
    }

    /**
     * 强制 无作用覆写
     * @param user 根据id
     * @return ...
     *
     */
    @Override
    public Integer updateTo(User user) {
        User user1 = new User();
        user1.setId(user.getId());
        user1.setName(user.getName());
        user1.setEmail(user.getEmail());
        user1.setAge(user.getAge());
        return userMapper.updateById(user1);
    }


    @Override
    public List<User> show() {
        List<User> users = userMapper.selectList(null);
        // 拦截信息 进行格式更改
        System.out.println();
        return users;
    }
}

